package org.itsolutions.javaBackup.gui.web.admin;

import java.net.URL;
import java.util.Locale;
import java.util.Properties;

import org.itsolutions.javaBackup.gui.web.admin.view.AdminMainView;
import org.itsolutions.javaBackup.gui.web.admin.view.AdminMenu;
import org.itsolutions.javaBackup.gui.web.common.ApplicationDataHolder;
import org.itsolutions.javaBackup.gui.web.common.EGUIApplication;
import org.itsolutions.javaBackup.gui.web.common.IAutenticatedApplication;
import org.itsolutions.javaBackup.gui.web.common.components.JBEmbedded;
import org.itsolutions.javaBackup.gui.web.common.components.JBPanel;
import org.itsolutions.javaBackup.gui.web.common.components.JBWindow;
import org.itsolutions.javaBackup.gui.web.common.composites.LayoutComposite;
import org.itsolutions.javaBackup.gui.web.common.composites.LoginComposite;
import org.itsolutions.javaBackup.logger.ELogLevel;
import org.itsolutions.javaBackup.logger.ILogger;
import org.itsolutions.javaBackup.logger.LoggerHelper;

import com.google.inject.Inject;
import com.vaadin.Application;
import com.vaadin.service.ApplicationContext;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.ThemeResource;

/**
 * Java-backup administration application. 
 * @author Tomas Fecko
 */
public final class AdministrationApplication extends Application implements IAutenticatedApplication {

	private static final long 		serialVersionUID = -3300734970227447357L;
	private static final ILogger 	LOGGER = LoggerHelper.getLogger(AdministrationApplication.class); 

	@Inject AdminMainView 			mMainPanel;
	@Inject ApplicationDataHolder 	mDataHolder;
	@Inject AdminMenu				mMenu;
	@Inject JBPanel 				mTopPanel;
	@Inject JBWindow				mMainWindow;
	@Inject LoginComposite 			mLoginWindow;
	@Inject LayoutComposite			mMainLayoutComposite;
	@Inject JBEmbedded 				mLogo;
	
	
	@Override
	public void init() {
		mDataHolder.setApplication(this);
		mDataHolder.setApplicationAndLocale(EGUIApplication.ADMIN, getLocale());
		setMainWindow(mMainWindow);
		mMainWindow.setContent(mLoginWindow.init());
		setTheme("jbTheme");
	}
	
	@Override
	public void start(URL pApplicationUrl, Properties pApplicationProperties, ApplicationContext pContext) {
		if (pApplicationProperties != null) {
			pApplicationProperties.put(EGUIApplication.class.getName(), EGUIApplication.ADMIN.name());
		}
		super.start(pApplicationUrl, pApplicationProperties, pContext);
	}

	@Override
	public void logout() {
		close();
		mMainWindow.open(new ExternalResource("/admin"));
	}
	
	@Override
	public void close() {
		super.close();
	}
	
	@Override
	public boolean login(String pLogin, String pPassword) {
		return ("admin".equals(pLogin) && "admin".equals(pPassword)) || ("".equals(pLogin) && "".equals(pPassword));
	}

	@Override
	public void initProtected() {
		try {
			Locale locale = getLocale(); //TODO retrieve from db based on user config
			mDataHolder.setApplicationAndLocale(EGUIApplication.ADMIN, locale);
			mMainWindow.setCaption("JB.TITLE.WND");
			mMainLayoutComposite.setParentComponent(mMainWindow).init();
			mLogo.setSource(new ThemeResource("images/logo.jpg"));
			
			mTopPanel.setScrollable(false);
			
			mMainLayoutComposite.setLogo(mLogo);
			mMainLayoutComposite.setTop(mTopPanel);
			mMainLayoutComposite.setMenu(mMenu.init().getMainContainer());
			mMainLayoutComposite.setMain(mMainPanel.init().getMainContainer());
			
			mMainWindow.setContent(mMainLayoutComposite);
		} catch (Exception e) {
			LOGGER.log(ELogLevel.ERROR, e);
		}
	}
}






